package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.math.BigDecimal;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.come.Frame.ZhuFrame;
import org.come.bean.GiveGoodsBean;
import org.come.bean.LoginResult;
import org.come.entity.Goodstable;
import org.come.entity.RoleSummoning;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.GsonUtil;
import org.come.until.Util;

import com.tool.Document.NumberDocument;
import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.RoleCaoZuoBtn;
import com.tool.role.RoleData;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;



//TODO 捐款面板

/**
 * @author 黄建彬 捐款面板
 */
public class DonationsJpanel extends JPanel {
    // 两个输入框
    private static JTextField giveSumMoney;
    private RoleCaoZuoBtn sureGive, deleteGive;// 确定捐款，取消

    public DonationsJpanel() throws Exception {
        String style = MyIsif.getStyle();
        if ("水墨UI".equals(style)) {
        // 键盘监听
        this.setPreferredSize(new Dimension(296, 257));
        this.setBackground(UIUtils.Color_BACK);
        this.setLayout(null);

        // 关闭按钮事件
//            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 40);
//            offBtn.setBounds(296 - 37, 10, 25, 25);
//            this.add(offBtn);

        giveSumMoney = new JTextField();// 捐款的钱数
        giveSumMoney.setBounds(128, 78, 128, 20);
        giveSumMoney.setOpaque(false);
        giveSumMoney.setBorder(BorderFactory.createEmptyBorder());
        giveSumMoney.setForeground(Color.red);
        giveSumMoney.setCaretColor(Color.white);
        giveSumMoney.addKeyListener(new KeyAdapter() {

            // 键盘监听
            @Override
            public void keyTyped(KeyEvent e) {

                int keyChar = e.getKeyChar();

                // 让该输入框只能输入数字
                if (keyChar < KeyEvent.VK_0 || keyChar > KeyEvent.VK_9) {
                    e.consume();
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {

            }
        });
        this.add(giveSumMoney);

        sureGive = new RoleCaoZuoBtn("skin/8100226/newbutton/B20.png", 1, "确认", 40, UIUtils.COLOR_BLACK);
        sureGive.setBounds(57, 110, 59, 24);
        this.add(sureGive);
        deleteGive = new RoleCaoZuoBtn("skin/8100226/newbutton/B20.png", 1, "取消", 41, UIUtils.COLOR_BLACK);
        deleteGive.setBounds(157, 110, 59, 24);
        this.add(deleteGive);
        }else {

            // 键盘监听
            this.setPreferredSize(new Dimension(267, 221));
            this.setBackground(UIUtils.Color_BACK);
            this.setLayout(null);

            giveSumMoney = new JTextField();// 捐款的钱数
            giveSumMoney.setBounds(100, 121, 128, 20);
            giveSumMoney.setOpaque(false);
            giveSumMoney.setBorder(BorderFactory.createEmptyBorder());
            giveSumMoney.setForeground(Color.white);
            giveSumMoney.setCaretColor(Color.white);
            giveSumMoney.setFont(new Font("宋体",Font.PLAIN,15));
            giveSumMoney.addKeyListener(new KeyListener() {

                // 键盘监听1
                @Override
                public void keyTyped(KeyEvent e) {
                    int keyChar = e.getKeyChar();
                    // 让该输入框只能输入数字
                    if (keyChar < KeyEvent.VK_0 || keyChar > KeyEvent.VK_9 ) {
                        e.consume();
                        return;
                    }

                }

                @Override
                public void keyReleased(KeyEvent e) {
                    // 捐的钱必须比玩家的钱少或相等否则自动设置为最大金币
                    if (giveSumMoney.getText() != null && giveSumMoney.getText().length() > 0) {
                        if(RoleData.getRoleData().getLoginResult().getGold().compareTo(new BigDecimal(giveSumMoney.getText())) == -1 ) {
                            giveSumMoney.setFont(new Font("宋体",Font.PLAIN,15));
                            giveSumMoney.setText(RoleData.getRoleData().getLoginResult().getGold().toString());
                        } else {
                            //按金额设置颜色
                            giveSumMoney.setForeground(Color.white);
                            if(giveSumMoney.getText().length() > 6) {
                                giveSumMoney.setForeground(Color.YELLOW);
                            }
                            if(giveSumMoney.getText().length() > 7) {
                                giveSumMoney.setForeground(UIUtils.COLOR_NAME2);
                            }
                            if(giveSumMoney.getText().length() > 8) {
                                giveSumMoney.setForeground(Color.GREEN);
                            }
                            if(giveSumMoney.getText().length() > 9) {
                                giveSumMoney.setForeground(Color.RED);
                            }
                        }
                    }
                }

                @Override
                public void keyPressed(KeyEvent e) {

                }
            });
            this.add(giveSumMoney);

            sureGive = new RoleCaoZuoBtn("inkImg/hongmu/6026.png", 1, "确认", 40, UIUtils.COLOR_BTNPUTONG2);
            sureGive.setBounds(62, 170, 68, 26);
            this.add(sureGive);
            deleteGive = new RoleCaoZuoBtn("inkImg/hongmu/6026.png", 1, "取消", 41, UIUtils.COLOR_BTNPUTONG2);
            deleteGive.setBounds(162, 170, 68, 26);
            this.add(deleteGive);
        }
    }

    /**
     * 帮派捐钱确定
     */
    public static void giveGangMoney() {
        LoginResult loginResult = RoleData.getRoleData().getLoginResult();
        if (giveSumMoney.getText() == null || giveSumMoney.getText().length() == 0) {
            ZhuFrame.getZhuJpanel().addPrompt2("为了帮派更好的发展别犹豫了先捐上一个亿");
            return;
        }
        BigDecimal money = new BigDecimal(giveSumMoney.getText());
        if (money == null) {
            ZhuFrame.getZhuJpanel().addPrompt2("想什么呢？你的兜里比脸上还干净");
            return;
        }
        int sy = loginResult.getGold().subtract(money).compareTo(new BigDecimal(0));
        if (sy == -1) {
            ZhuFrame.getZhuJpanel().addPrompt2("自己有多少钱心里没点数吗？");
            return;
        }
        String senmes = Agreement.givemoneyAgreement(money.toString());
        SendMessageUntil.toServer(senmes);//向服务器发送信息
        loginResult.setGold(loginResult.getGold().subtract(money));
        ZhuFrame.getZhuJpanel().addPrompt("扣除了" + money + "金币");
        giveSumMoney.setText("");
    }

    private ImageIcon icon;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        String style = MyIsif.getStyle();
        if ("水墨UI".equals(style)) {
        if (icon == null) {
            icon = new ImageIcon("inkImg/old/background/S142.png");
        }
        g.drawImage(icon.getImage(), 0, 0, 296, 257, this);
        Util.drawMoney(g, 128, 33 + 15);
        }else {
            if (icon == null)
                icon = new ImageIcon("img/xy2uiimg/86_png.xy2uiimg.donate.png");
            g.drawImage(icon.getImage(), 0, 0, 267, 221, this);
            Util.drawMoney(g, 100, 33 + 50);
        }
    }

    public RoleCaoZuoBtn getSureGive() {
        return sureGive;
    }

    public void setSureGive(RoleCaoZuoBtn sureGive) {
        this.sureGive = sureGive;
    }

    public RoleCaoZuoBtn getDeleteGive() {
        return deleteGive;
    }

    public void setDeleteGive(RoleCaoZuoBtn deleteGive) {
        this.deleteGive = deleteGive;
    }

    public static JTextField getGiveSumMoney() {
        return giveSumMoney;
    }

    public static void setGiveSumMoney(JTextField giveSumMoney) {
        DonationsJpanel.giveSumMoney = giveSumMoney;
    }

}
